package Ritsu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class PersonalFinanceManager {

    private List<String> incomeList;  
    private List<String> expenseList; 

    public PersonalFinanceManager() {
        incomeList = new ArrayList<>();
        expenseList = new ArrayList<>();
    }
    public void addIncome(String date, double amount, String category, String remark) {
        incomeList.add("日期: " + date + ", 金额: " + amount + ", 类别: " + category + ", 备注: " + remark);
    }

    public void addExpense(String date, double amount, String category, String remark) {
        expenseList.add("日期: " + date + ", 金额: " + amount + ", 类别: " + category + ", 备注: " + remark);
    }

    public void showAllRecords() {  \\添加收入支出功能
        System.out.println("收入记录：");
        for (String income : incomeList) {
            System.out.println(income);
        }

        System.out.println("支出记录：");
        for (String expense : expenseList) {
            System.out.println(expense);
        }
    }

    public double getTotalIncome() {
        double totalIncome = 0;
        for (String record : incomeList) {
            String[] parts = record.split(", ");
            totalIncome += Double.parseDouble(parts[1].split(": ")[1]);
        }
        return totalIncome;
    }

    public double getTotalExpense() {
        double totalExpense = 0;
        for (String record : expenseList) {
            String[] parts = record.split(", ");
            totalExpense += Double.parseDouble(parts[1].split(": ")[1]);
        }
        return totalExpense;
    }

    public double getBalance() {
        return getTotalIncome() - getTotalExpense();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        PersonalFinanceManager manager = new PersonalFinanceManager();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        while (true) {
            System.out.println("欢迎使用个人账单管理系统");
            System.out.println("1. 记录收入");
            System.out.println("2. 记录支出");
            System.out.println("3. 查询所有记录");
            System.out.println("4. 查询总收入");
            System.out.println("5. 查询总支出");
            System.out.println("6. 查询余额");
            System.out.println("7. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine();  
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

class Record {
    private Date date;
    private double amount;
    private String category;
    private String note;

    public Record(Date date, double amount, String category, String note) {
        this.date = date;
        this.amount = amount;
        this.category = category;
        this.note = note;
    }

    public Date getDate() {
        return date;
    }

    public double getAmount() {
        return amount;
    }

    public String getCategory() {
        return category;
    }

    public String getNote() {
        return note;
    }

    @Override
    public String toString() {
        return String.format("Date: %s, Amount: %.2f, Category: %s, Note: %s", 
                             date.toString(), amount, category, note);
    }
}

public class PersonalFinanceManager {
    private ArrayList<Record> incomeRecords;
    private ArrayList<Record> expenseRecords;

    public PersonalFinanceManager() {
        incomeRecords = new ArrayList<>();
        expenseRecords = new ArrayList<>();
    }

    // 添加收入记录
    public void addIncome(double amount, String category, String note) {
        if (amount <= 0) {
            System.out.println("收入金额必须为正数！");
            return;
        }
        incomeRecords.add(new Record(new Date(), amount, category, note));
        System.out.println("收入记录已添加！");
    }

    // 添加支出记录
    public void addExpense(double amount, String category, String note) {
        if (amount <= 0) {
            System.out.println("支出金额必须为正数！");
            return;
        }
        expenseRecords.add(new Record(new Date(), amount, category, note));
        System.out.println("支出记录已添加！");
    }

    // 查询收入记录
    public void showIncomeRecords() {
        if (incomeRecords.isEmpty()) {
            System.out.println("没有任何收入记录！");
            return;
        }
        System.out.println("收入记录：");
        for (Record record : incomeRecords) {
            System.out.println(record);11
        }
    }

    // 查询支出记录
    public void showExpenseRecords() {
        if (expenseRecords.isEmpty()) {
            System.out.println("没有任何支出记录！");
            return;
        }
        System.out.println("支出记录：");
        for (Record record : expenseRecords) {
            System.out.println(record);
        }
    }

    // 查询所有账单（收入 + 支出）
    public void showAllRecords() {
        if (incomeRecords.isEmpty() && expenseRecords.isEmpty()) {
            System.out.println("没有任何账单记录！");
            return;
        }

        System.out.println("所有账单记录：");
        if (!incomeRecords.isEmpty()) {
            System.out.println("收入记录：");
            for (Record record : incomeRecords) {
                System.out.println(record);
            }
        }

        if (!expenseRecords.isEmpty()) {
            System.out.println("支出记录：");
            for (Record record : expenseRecords) {
                System.out.println(record);
            }
        }
    }

    // 查询特定账单（通过金额或者类别）
    public void searchRecords(String category, double amount) {
        boolean found = false;

        System.out.println("查询结果：");
        // 查询收入记录
        for (Record record : incomeRecords) {
            if ((category == null || record.getCategory().equalsIgnoreCase(category)) && 
                (amount <= 0 || record.getAmount() == amount)) {
                System.out.println("收入 - " + record);
                found = true;
            }
        }

        // 查询支出记录
        for (Record record : expenseRecords) {
            if ((category == null || record.getCategory().equalsIgnoreCase(category)) && 
                (amount <= 0 || record.getAmount() == amount)) {
                System.out.println("支出 - " + record);
                found = true;
            }
        }

        if (!found) {
            System.out.println("没有找到符合条件的记录。");
        }
    }

    // 统计收入总额
    public void totalIncome() {
        double total = 0;
        for (Record record : incomeRecords) {
            total += record.getAmount();
        }
        System.out.printf("总收入：%.2f\n", total);
    }

    // 统计支出总额
    public void totalExpense() {
        double total = 0;
        for (Record record : expenseRecords) {
            total += record.getAmount();
        }
        System.out.printf("总支出：%.2f\n", total);
    }

    // 计算当前余额
    public void calculateBalance() {
        double totalIncome = 0;
        for (Record record : incomeRecords) {
            totalIncome += record.getAmount();
        }

        double totalExpense = 0;
        for (Record record : expenseRecords) {
            totalExpense += record.getAmount();
        }

        double balance = totalIncome - totalExpense;
        System.out.printf("当前余额：%.2f\n", balance);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        PersonalFinanceManager manager = new PersonalFinanceManager();

        while (true) {
            System.out.println("\n个人账单管理系统");
            System.out.println("1. 录入收入");
            System.out.println("2. 录入支出");
            System.out.println("3. 查询收入记录");
            System.out.println("4. 查询支出记录");
            System.out.println("5. 查看所有账单");
            System.out.println("6. 查询特定账单");
            System.out.println("7. 统计总收入");
            System.out.println("8. 统计总支出");
            System.out.println("9. 计算余额");
            System.out.println("10. 退出");
            System.out.print("请输入选项：");

            int choice = scanner.nextInt();
            scanner.nextLine();  // 读取多余的换行符

            switch (choice) {
                case 1:
                    System.out.print("请输入收入金额：");
                    double incomeAmount = scanner.nextDouble();
                    scanner.nextLine(); // 清除换行符
                    System.out.print("请输入收入类别：");
                    String incomeCategory = scanner.nextLine();
                    System.out.print("请输入收入备注：");
                    String incomeNote = scanner.nextLine();
                    manager.addIncome(incomeAmount, incomeCategory, incomeNote);
                    break;
                case 2:
                    System.out.print("请输入支出金额：");
                    double expenseAmount = scanner.nextDouble();
                    scanner.nextLine(); // 清除换行符
                    System.out.print("请输入支出类别：");
                    String expenseCategory = scanner.nextLine();
                    System.out.print("请输入支出备注：");
                    String expenseNote = scanner.nextLine();
                    manager.addExpense(expenseAmount, expenseCategory, expenseNote);
                    break;
                case 3:
                    manager.showIncomeRecords();
                    break;
                case 4:
                    manager.showExpenseRecords();
                    break;
                case 5:
                    manager.showAllRecords();
                    break;
                case 6:
                    System.out.print("请输入查询类别（可选）：");
                    String searchCategory = scanner.nextLine();
                    System.out.print("请输入查询金额（0为不限）：");
                    double searchAmount = scanner.nextDouble();
                    manager.searchRecords(searchCategory, searchAmount);
                    break;
                case 7:
                    manager.totalIncome();HAHAHA
                    break;
                case 8:
                    manager.totalExpense();
                    break;
                case 9:
                    manager.calculateBalance();
                    break;
                case 10:
                    System.out.println("退出系统。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选项，请重新选择！");
            }
        }
    }
}

            switch (choice) {
                case 1:
                    System.out.print("请输入收入日期 (yyyy-MM-dd): ");
                    String incomeDate = scanner.nextLine();
                    double incomeAmount = getPositiveAmount(scanner);
                    System.out.print("请输入类别：");
                    String incomeCategory = scanner.nextLine();
                    System.out.print("请输入备注：");
                    String incomeRemark = scanner.nextLine();
                    manager.addIncome(incomeDate, incomeAmount, incomeCategory, incomeRemark);
                    System.out.println("收入记录已保存！");
                    break;

                case 2:
                    System.out.print("请输入支出日期 (yyyy-MM-dd): ");
                    String expenseDate = scanner.nextLine();
                    double expenseAmount = getPositiveAmount(scanner);
                    System.out.print("请输入类别：");
                    String expenseCategory = scanner.nextLine();
                    System.out.print("请输入备注：");
                    String expenseRemark = scanner.nextLine();
                    manager.addExpense(expenseDate, expenseAmount, expenseCategory, expenseRemark);
                    System.out.println("支出记录已保存！");
                    break;

                case 3:
                    manager.showAllRecords();
                    break;

                case 4:
                    System.out.println("总收入: " + manager.getTotalIncome());
                    break;

                case 5:
                    System.out.println("总支出: " + manager.getTotalExpense());
                    break;

                case 6:
                    System.out.println("当前余额: " + manager.getBalance());
                    break;

                case 7:
                    System.out.println("谢谢使用，再见！");
                    scanner.close();
                    System.exit(0);
                    break;

                default:
                    System.out.println("无效选项，请重新选择！");
            }
        }
    }

    private static double getPositiveAmount(Scanner scanner) {
        double amount;
        while (true) {
            System.out.print("请输入金额：");
            amount = scanner.nextDouble();
            if (amount > 0) {
                return amount;
            } else {
                System.out.println("金额必须为正数，请重新输入！");
            }
        }
    }
}
